package org.judo.generate.mvc;

public class PropParser implements Parser
{
	String curStatement;
	ModelResult modelResult;
	
	public ModelResult getModelResult()
	{
		return modelResult;
	}

	public void setModelResult(ModelResult modelResult)
	{
		this.modelResult = modelResult;
	}

	public Parser getNestedParser()
	{
		return null;
	}

	public boolean isFinished()
	{
		if (curStatement != null)
			if (curStatement.equals("/props"))
				return true;
				
		return false;
	}

	public boolean needsNestedParser()
	{
		return false;
	}

	public void parseNextLine(String line) 
		throws ParseException
	{
		curStatement = line;
		if (line.startsWith("/props"))
		{
		}
		else if (line.startsWith("int:"))
		{
			PropertyResult result = new PropertyResult();
			result.type = "int";
			parsePropertyt(result,line);
			modelResult.props.put(result.name,result);
		}
		else if (line.startsWith("string:"))
		{
			PropertyResult result = new PropertyResult();
			result.type = "String";
			parsePropertyt(result,line);
			modelResult.props.put(result.name,result);
		}
		else if (line.startsWith("date:"))
		{
			PropertyResult result = new PropertyResult();
			result.type = "date";
			parsePropertyt(result,line);
			modelResult.props.put(result.name,result);
		}
		else if (line.startsWith("double:"))
		{
			PropertyResult result = new PropertyResult();
			result.type = "double";
			parsePropertyt(result,line);
			modelResult.props.put(result.name,result);
		}
		else if (line.startsWith("char:"))
		{
			PropertyResult result = new PropertyResult();
			result.type = "char";
			parsePropertyt(result,line);
			modelResult.props.put(result.name,result);
		}
		else if (line.equals(""))
		{
		}
		else
		{
			throw new ParseException("Expected property declaration type, . '"+line+"' instead.");
		}
		 
	}

	private void parsePropertyt(PropertyResult result, String line) 
		throws ParseException
	{
		String parts[] = line.split(":");
		String params[] = parts[1].split(",");
		
		if (params.length == 2)
		{
			result.name = params[0].trim();
			result.display = params[1].trim();
		}
		else if (params.length == 3)
		{
			result.name = params[0].trim();
			result.display = params[1].trim();
			
			if (params[2].trim().equals("pk"))
				result.isPK = true;
			
			if (params[2].trim().equals("auto"))
				result.isPK = true;
		}
		else if (params.length == 4)
		{
			result.name = params[0].trim();
			result.display = params[1].trim();
			
			if (params[2].trim().equals("pk"))
				result.isPK = true;
			
			if (params[2].trim().equals("auto"))
				result.isPK = true;
			
			if (params[3].trim().equals("pk"))
				result.isPK = true;
			
			if (params[3].trim().equals("auto"))
				result.isPK = true;
		}
		else
			throw new ParseException("Wrong number of parameters for property declaration");
	}

	public void setInitLine(String line) throws ParseException
	{
	}
}
